package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.SecneGroup;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class SecneGroupDao {
    private Context mContext;
    private Dao<SecneGroup, Integer> groupDaoOpe;
    private DatabaseHelper helper;

    public SecneGroupDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            groupDaoOpe = helper.getDao(SecneGroup.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(SecneGroup group) {
        try {
            group.setChecked(false);
            groupDaoOpe.create(group);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateGroup(SecneGroup group) {
        try {
            group.setChecked(false);
            return groupDaoOpe.update(group);
            //    LogHelper.i("updateLightGroupnumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public SecneGroup get(int id) {
        try {
            return groupDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<SecneGroup> getSecneGroups(int secneId) {
        List<SecneGroup> groupList = new ArrayList<>();
        try {
            groupList = groupDaoOpe.queryBuilder().where().eq
                    ("secneId", secneId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groupList;
    }

    /**
     * @return
     */
    public List<SecneGroup> getAll() {
        List<SecneGroup> groupList = new ArrayList<>();
        try {
            groupList = groupDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groupList;
    }

    public SecneGroup getById(int groupId) {
        List<SecneGroup> groupList = null;
        try {
            groupList = groupDaoOpe.queryBuilder().where().eq
                    ("ballLightId", groupId).query();
            if (groupList != null && groupList.size() > 0) {
                return groupList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<SecneGroup> getFamilyGroups(String familyId) {
        List<SecneGroup> groupList = new ArrayList<>();
        try {
            groupList = groupDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return groupList;
    }

    public int delete(SecneGroup group) {
        try {
            return groupDaoOpe.delete(group);
            //    LogHelper.i("deleLightGroupnumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }


    public void deleteAll() {
        try {
            int number = groupDaoOpe.deleteBuilder().delete();
         LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<SecneGroup> groupList) {
        if (groupList==null)return;
        try {

            int number = groupDaoOpe.delete(groupList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteFamilySecneGroup(String familyId) {
        try {
            List<SecneGroup> familyGroups = getFamilyGroups(familyId);
            int number = groupDaoOpe.delete(familyGroups);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public void deleteSecneMode(int sceneId) {
        try {
            List<SecneGroup> secneGroups = getSecneGroups(sceneId);
            delete(secneGroups);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
